
#include "area.h"
#include "avatar.h"
#include "commandTable.h"
#include "commandTable-default.h"
#include "display.h"
#include "handler.h"
#include "room.h"
#include "symphony.h"
#include "world.h"

/*
Cmd::Cmd( void ) {
  name( "" );
  return;
}

bool Cmd::execute( Creature* creature, const std::string& args ) {

  return true;
}
*/

CmdTell::CmdTell( void ) {
  name( "tell" );
  syntax( "tell <target> <message>" );
  return;
}

bool CmdTell::execute( Creature* creature, const std::string& args ) {
  Creature* target = NULL;
  std::vector<std::string> pieces = Regex::split( args, 1 );

  if ( args.empty() ) {
    creature->channel( CHAN_TELL, !creature->channel( CHAN_TELL ) );
    if ( creature->channel( CHAN_TELL ) ) {
      creature->send( "Tell channel [{GON{x]" );
      return true;
    } else {
      creature->send( "Tell channel [{rOFF{x]" );
      return true;
    }
  }

  if ( ( target = World::Instance().findAvatar( pieces[0] ) ) == NULL ) {
    creature->send( "They're not around at the moment." );
    return false;
  } else {
    if ( !creature->canSee( target ) == SEE_NAME ) {
      creature->send( "They're not around at the moment." );
      return false;
    }
    if ( !target->channel( CHAN_TELL ) ) {
      creature->send( "They're not receiving tells right now." );
      return false;
    }
    target->replyTo( creature->name() );
    target->send( "%s tells you \"{Y%s{x\"", target->seeName( creature, true ).c_str(), pieces[1].c_str() );
    creature->send( "You tell %s \"{Y%s{x\"", target->name().c_str(), pieces[1].c_str() );
  }

  return true;
}

CmdTime::CmdTime( void ) {
  name( "time" );
  return;
}

bool CmdTime::execute( Creature* creature, const std::string& args ) {
  std::string output;

  output.append( "Blah blah blah - IC time goeth here, yo\n\n" );
  output.append( "---------------------------------------------------------\n" );
  output.append( "  System time:   " ).append( World::strtime() ).append( "\n" );
  output.append( "  Server booted: " ).append( World::strtime( World::Instance().booted() ) ).append( "\n" );
  output.append( "  Server online for " ).append( World::realtime( World::now() - World::Instance().booted() ) );

  creature->send( output );

  return true;
}

CmdTimers::CmdTimers( void ) {
  name( "timers" );
  level( GOD );
  return;
}

bool CmdTimers::execute( Creature* creature, const std::string& args ) {
  Timer* t = NULL;
  std::vector<Timer*>::iterator it;
  char buffer[MAX_BUFFER];
  std::string output;

  output.append( "                    Scheduled Events\n" );
  sprintf( buffer, "  %-15s%-15s\n  ----------------------------------------------------------------------", "Name", "Next Fire" );
  output.append( buffer );

  for ( it = World::Instance().getTimers().begin(); it != World::Instance().getTimers().end(); ++it ) {
    t = *it;
    sprintf( buffer, "\n  %-15s%-25s", t->name().c_str(), World::realtime( t->when()-World::now() ).c_str() );
    output.append( buffer );
  }

  creature->send( output );

  return true;
}

CmdUnlock::CmdUnlock( void ) {
  name( "unlock" );
  syntax( "syntax: unlock <direction>" );
  return;
}

bool CmdUnlock::execute( Creature* creature, const std::string& args ) {
  Exit* exit = NULL;
  Object* key = NULL;

  if ( ( exit = creature->room()->exit( args ) ) == NULL ) {
    creature->send( "There is no door in that direction." );
    return false;
  }

  if ( !exit->flag( EFLAG_CLOSED ) ) {
    creature->send( "It's open." );
    return false;
  }

  if ( !exit->flag( EFLAG_LOCKED ) ) {
    creature->send( "It's already unlocked." );
    return false;
  }

  for ( std::list<Object*>::iterator it = creature->objects().begin(); it != creature->objects().end(); ++it ) {
    if ( (*it)->vnum() == exit->key() ) {
      key = *it;
      break;
    }
  }

  if ( key == NULL || key->type() != OTYPE_KEY ) {
    creature->send( "You don't have the key." );
    return false;
  }

  exit->flag( EFLAG_LOCKED, false );
  creature->send( "You unlock the door." );
  creature->room()->send_cond( "$p unlocked the way $e.", creature, exit );

  return true;
}

CmdWho::CmdWho( void ) {
  name( "who" );
  return;
}

bool CmdWho::execute( Creature* creature, const std::string& args ) {
  estring output;

  output.append( "     Players Online\n" );
  for ( std::map<std::string,Avatar*>::const_iterator it = World::Instance().getAvatars().begin(); it != World::Instance().getAvatars().end(); ++it ) {
    if ( creature->canSee( it->second ) == SEE_NAME ) {
      output.append( "  " ).append( it->second->name() ).append( "\n" );
    }
  }

  creature->send( output );

  return true;
}

CmdWhoAmI::CmdWhoAmI( void ) {
  name( "whoami" );
  return;
}

bool CmdWhoAmI::execute( Creature* creature, const std::string& args ) {
  creature->send( "You are logged in as %s.", creature->name().c_str() );
  return true;
}

